﻿//	Ordinaire.Security.Cryptographer
//	Copyright (c) 2011 Jonathan Loe
//
//	MIT license (http://en.wikipedia.org/wiki/MIT_License)
//
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights 
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
//	of the Software, and to permit persons to whom the Software is furnished to do so, 
//	subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in all 
//	copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
//	INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
//	PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
//	FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
//	ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace Ordinaire.Security
{
    /// <summary>
    /// Represents a cryptography helper class.
    /// </summary>
    public class Cryptographer
    {
        #region Constructor

        /// <summary>
        /// Hidden constructor
        /// </summary>
        private Cryptographer() { }

        #endregion

        #region Public methods

        /// <summary>
        /// Hash specified input using SHA256.
        /// </summary>
        /// <param name="input">input text</param>
        /// <returns>hashing value in hexadecimal</returns>
        public static string Hash(string input)
        {
            return Hash(input, SHA256.Create());
        }

        /// <summary>
        /// Hash specified input using specified hash algorithm.
        /// </summary>
        /// <param name="input">input text</param>
        /// <param name="algorithm">hash algorithm</param>
        /// <returns>hashing value in hexadecimal</returns>
        public static string Hash(string input, HashAlgorithm algorithm)
        {
            if (String.IsNullOrEmpty(input))
            {
                throw new ArgumentNullException("input");
            }

            // Convert the input string to a byte array and compute the hash.
            byte[] data = algorithm.ComputeHash(Encoding.UTF8.GetBytes(input));

            // Create a new Stringbuilder to collect the bytes
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data 
            // and format each one as a hexadecimal string.
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            // Return the hexadecimal string.
            return sBuilder.ToString();
        }

        /// <summary>
        /// Verify hash value against the specified input using MD5.
        /// </summary>
        /// <param name="input">input text</param>
        /// <param name="hash">hashing value</param>
        /// <returns>true if hashing result of the input matches with the specified hashing value</returns>
        public static bool VerifyHash(string input, string hash)
        {
            MD5 md5Hash = MD5.Create();
            return VerifyHash(input, hash, md5Hash);
        }

        /// <summary>
        /// Verify hash value against the specified input using specified hash algorithm.
        /// </summary>
        /// <param name="input">input text</param>
        /// <param name="hash">hashing value</param>
        /// <param name="algorithm">hash algorithm</param>
        /// <returns>true if hashing result of the input matches with the specified hashing value</returns>
        public static bool VerifyHash(string input, string hash, HashAlgorithm algorithm)
        {
            if (String.IsNullOrEmpty(input))
            {
                throw new ArgumentNullException("input");
            }

            if (String.IsNullOrEmpty(hash))
            {
                throw new ArgumentNullException("hash");
            }

            // Hash the input.
            string hashOfInput = Hash(input, algorithm);

            // Create a StringComparer an compare the hashes.
            StringComparer comparer = StringComparer.OrdinalIgnoreCase;

            if (0 == comparer.Compare(hashOfInput, hash))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        #endregion
    }
}
